﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2008
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

import andromeda.controller.AbstractController;

import asgard.config.Config;
import asgard.display.DisplayObjectCollector;
import asgard.system.Lang;
import asgard.system.Locale;
import asgard.system.Localization;

import astro.display.UIList;
import astro.display.application.MyDisplay;

import mars.logging.ApplicationLogger;

import vegas.events.Event;
import vegas.events.EventDispatcher;

/**
 * This controller initialize the application when all externals assets and configs are loading and initialize.
 * @author eKameleon
 */
class astro.controller.application.InitApplication extends AbstractController
{
	
	/**
	 * Creates a new InitApplication instance.
	 */
	public function InitApplication() 
	{
		super() ;
	}
	
	/**
	 * Handles the event.
	 */
	public function handleEvent(e:Event) 
	{
		
		if ( Config.getInstance().verbose)
		{
			_debug() ;
		}
		
		getLogger().debug ( this + " handleEvent.") ;
		
		initGlobals() ;
		
		MyDisplay(DisplayObjectCollector.get(UIList.MY_DISPLAY)).show() ;
	}


	/**
	 * Initialize the globals methods of the application.
	 */
	public static function initGlobals():Void
	{
		_global.CONFIG     = Config.getInstance() ;
		_global.DISPATCHER = EventDispatcher.getInstance() ;
		_global.LOGGER     = ApplicationLogger ;
	}
		
	/**
	 * This internal method launch the debug mode if the {@code Config.getInstance().verbose} property is {@code true}
	 */
	private function _debug():Void
	{
		
		// View in the logs the default Configuration content
		
		var conf:Config = Config.getInstance() ;
		var each:String ;
		var prop:String ;
		getLogger().debug( this + " config") ;
		var space:String = "   " ;
		for (each in conf) 
		{
			getLogger().info( space + "+ " + each + " : " + conf[each]) ;
			for ( prop in conf[each]) 
			{
				getLogger.info( space + space + "+ " + prop + " : " + conf[each][prop]) ;
			}
		}
		
		// View in the logs the default localization content 
		
		var target:Localization = Localization.getInstance();
		var current:Lang = target.getCurrent() ;
		var locale:Locale = target.getLocale() ;

		getLogger().debug( this + " localization : " + current ) ;
		for ( each in locale) 
		{
			getLogger().info( space + "+ " + each + " : " + locale[each]) ;
			for ( prop in locale[each]) 
			{
				getLogger().info( space + space + "+ " + prop + " : " + locale[each][prop]) ;
			}
		}

	}
	
}